package cn.net.wanji.utils;

import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.HttpVersion;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.mime.MultipartEntity;
import org.apache.http.entity.mime.content.ContentBody;
import org.apache.http.entity.mime.content.FileBody;
import org.apache.http.entity.mime.content.StringBody;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.params.CoreProtocolPNames;
import org.apache.http.util.EntityUtils;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestTemplate;

import java.io.File;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author guodejun
 * @createTime 2022/11/8 11:23
 * @description
 */
public class HttpUtils {

    public static String upload(String url, List<File> files, Map<String, String> mapParams) {
        HttpClient client = new DefaultHttpClient();
        HttpPost httpPost = new HttpPost(url);
        client.getParams().setParameter(CoreProtocolPNames.PROTOCOL_VERSION, HttpVersion.HTTP_1_1);
        client.getParams().setParameter(CoreProtocolPNames.HTTP_CONTENT_CHARSET, "utf-8");
        try {
            MultipartEntity entity = new MultipartEntity();//多个表单对象
            for (File file : files) {
                ContentBody fileBody = new FileBody(file); //表单文件域
                entity.addPart("files", fileBody);
            }
            for (String key : mapParams.keySet()) {
                entity.addPart(key, new StringBody(mapParams.get(key)));
            }
            httpPost.setEntity(entity);
            HttpResponse response = client.execute(httpPost);// 执行post操作，并返回response
            if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
                return EntityUtils.toString(response.getEntity(), "UTF-8");
            } else {
                System.out.println("no response");
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public static String sendPostRequest(String url, Map<String, String> mapParams) {
        RestTemplate restTemplate = new RestTemplate();
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);
        MultiValueMap<String, String> map = new LinkedMultiValueMap<>();

        for (String key : mapParams.keySet()) {
            map.add(key, mapParams.get(key));
        }

        HttpEntity<MultiValueMap<String, String>> request = new HttpEntity<>(map, headers);
        ResponseEntity<String> response = restTemplate.postForEntity(url, request, String.class);
        return response.getBody();
    }


    public static void main(String[] args) {

        Map<String, String> mapParams = new HashMap<>();
        mapParams.put("jsonRoot", "{}");
        String s = sendPostRequest("http://127.0.0.1:9092/receive/data", mapParams);
        System.out.println(s);

        mapParams.clear();
        mapParams.put("DeviceInfo", null);
        s = sendPostRequest("http://127.0.0.1:9092/receive/deviceInfoData", mapParams);
        System.out.println(s);

        mapParams.clear();
        mapParams.put("deviceStatus", null);
        s = sendPostRequest("http://127.0.0.1:9092/receive/deviceStatusData", mapParams);
        System.out.println(s);


    }
}
